package com.wlc.base.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 测试lambda表达式
 */
public class TestLambda5 {

    public List<ProductDo> getProduct() {
        List<ProductDo> productDoList = new ArrayList<>();
        productDoList.add(new ProductDo("a", 23, 100));
        productDoList.add(new ProductDo("a", 23, 100));
        productDoList.add(new ProductDo("a", 23, 100));
        productDoList.add(new ProductDo("b", 23, 200));
        productDoList.add(new ProductDo("b", 23, 200));
        productDoList.add(new ProductDo("b", 23, 200));
        return productDoList;
    }

    /**
     * 按照类型分组，统计每组产品的总金额 sum之和
     */
    public void method01() {
        // 根据list中的数据，按照类型分组，并求和，把结果返回
        List<ProductDo> products = getProduct();
        List<ProductDo> collect = products.stream().collect(Collectors.toMap(ProductDo::getType, a -> a, (a1, a2) -> {
            a1.setSum(a1.getSum() + a2.getSum());
            return a1;
        })).values().stream().collect(Collectors.toList());
        collect.stream().forEach(System.out::print);
    }

    public static void main(String[] args) {
        TestLambda5 testLambda5 = new TestLambda5();
        testLambda5.method01();
    }
}

class ProductDo {

    // 类型
    private String type;
    // 价格
    private int price;
    // 合计
    private int sum;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public ProductDo() {
    }

    public ProductDo(String type, int price, int sum) {
        this.type = type;
        this.price = price;
        this.sum = sum;
    }

    @Override
    public String toString() {
        return "ProductDo{" +
                "type='" + type + '\'' +
                ", price=" + price +
                ", sum=" + sum +
                '}';
    }
}
